package io.filecopy;

import java.io.File;
import java.io.IOException;

public class TouchTask implements FileSystemTask {

  public TouchTask(File destination, LastModifyTimeMode lastModifyTimeMode, long sourceTime){
    this.destination = destination;
    this.lastModifyTimeMode = lastModifyTimeMode;
    this.sourceTime = sourceTime;
  }
  
  private LastModifyTimeMode lastModifyTimeMode;
  
  private File destination;

  private long sourceTime;

  @Override
  public void execute(ProgressLogger progress) throws IOException {
    switch (lastModifyTimeMode) {
    case KEEP_OR_CURRENT_TIME:
    case KEEP_OR_SOURCE_TIME:
      // keep current time
      break;
    case OVERWRITE_CURRENT_TIME:
      boolean success = destination.setLastModified(System.currentTimeMillis());
      if (success)
        progress.touchSucceded();
      else
        progress.touchFailed();
      break;
    case OVERWRITE_SOURCE_TIME:
      success = destination.setLastModified(sourceTime);
      if (success)
        progress.touchSucceded();
      else
        progress.touchFailed();
      break;
    default:
      throw new IllegalStateException("LastModifiedTimeMode "
          + lastModifyTimeMode + " has nnot been implemented yet.");
    }
  }
  
  @Override
  public String toString() {
    return "touch \"" + destination.getAbsolutePath() + "\" " + lastModifyTimeMode + " sourceTime=" + sourceTime;
  }
}